package com.career.syn.util;

import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 
* @ClassName: MD5Util
* @Description: md5工具类
* @author georgeli
* @date 2018年1月25日 下午5:11:28
 */
@SuppressWarnings("restriction")
public class MD5Util {
	
	private final static String KEY_MD5 = "MD5";

	public static String md5Base64(String s) {
		if (s == null)
			return null;
		String encodeStr = "";
		byte[] utfBytes = s.getBytes();
		MessageDigest mdTemp;
		try {
			mdTemp = MessageDigest.getInstance(KEY_MD5);
			mdTemp.update(utfBytes);
			byte[] md5Bytes = mdTemp.digest();
			BASE64Encoder b64Encoder = new BASE64Encoder();
			encodeStr = b64Encoder.encode(md5Bytes);
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
		return encodeStr;
	}

	public static String md5(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance(KEY_MD5);
			md.update(str.getBytes());
			StringBuffer sb = new StringBuffer();
			byte[] bytes = md.digest();
			for (int i = 0; i < bytes.length; i++) {
				int b = bytes[i] & 0xFF;
				if (b < 0x10) {
					sb.append('0');
				}
				sb.append(Integer.toHexString(b));
			}
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}